﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace DemoAstarAlgorithm {
    class cWitch : cSprite {
        public struct TTWitch {
            public Texture2D m_tWitch;
            public int m_iTimeSinceLastFrame;
            public int m_iMilliSecondsPerFrame;
            public List<Rectangle> m_rFrame;
            public int m_iIDFrame;
            public TTWitch(Texture2D newpicWitch, int mPF) {
                this.m_tWitch = newpicWitch;
                this.m_iTimeSinceLastFrame = 0;
                this.m_iMilliSecondsPerFrame = mPF;
                m_rFrame = new List<Rectangle>();
                this.m_iIDFrame = 0;
            }
        }

        private TTWitch m_ttWitchDie;
        public TTWitch m_ttWitchLight;
        public Texture2D m_tPet;
        public List<Rectangle> m_rFramePet { get; set; }
        public int m_iIDPet { get; set; }
        public List<Rectangle> m_rFrameWitch { get; set; }
        public int m_iDirection { get; set; }
        public int m_iIDWitch { get; set; }
        public int m_iCount = 0;
        public int m_iLife;
        public double m_dSpeed;
        public KeyboardState m_ksPreKeyState;
        public cWitch(Vector2 newPosition, ContentManager content)
            : base(content.Load<Texture2D>(@"images/witch"), newPosition) {
            m_ttWitchDie = new TTWitch(content.Load<Texture2D>(@"images/witch-die"), 180);
            m_ttWitchLight = new TTWitch(content.Load<Texture2D>(@"images/witch-light"), 150);
            m_tPet = content.Load<Texture2D>(@"images/pet");
            m_rFramePet = new List<Rectangle>();
            m_iIDPet = 0;
            m_rFrameWitch = new List<Rectangle>();
            m_iDirection = 0;
            m_iIDWitch = 0;
            m_iLife = 2;
            m_dSpeed = 2;
            CatHinh();
        }

        private void CatHinh() {

            m_rFrameWitch.Add(new Rectangle(0, 70, 45, 70));//Frame[0]
            m_rFrameWitch.Add(new Rectangle(45, 70, 45, 70)); //Frame[1]
            m_rFrameWitch.Add(new Rectangle(90, 70, 45, 70));//Frame[2]
            m_rFrameWitch.Add(new Rectangle(135, 70, 45, 70));//Frame[3]

            m_rFrameWitch.Add(new Rectangle(0, 0, 45, 70)); //Frame[4]
            m_rFrameWitch.Add(new Rectangle(45, 0, 45, 70)); //Frame[5]
            m_rFrameWitch.Add(new Rectangle(90, 0, 45, 70));//Frame[6]
            m_rFrameWitch.Add(new Rectangle(135, 0, 45, 70));//Frame[7]

            m_rFrameWitch.Add(new Rectangle(0, 140, 45, 70));//Frame[8]
            m_rFrameWitch.Add(new Rectangle(45, 140, 45, 70));//Frame[9]
            m_rFrameWitch.Add(new Rectangle(90, 140, 45, 70)); //Frame[10]
            m_rFrameWitch.Add(new Rectangle(135, 140, 45, 70));//Frame[11]

            m_rFrameWitch.Add(new Rectangle(0, 210, 45, 70));//Frame[12]
            m_rFrameWitch.Add(new Rectangle(45, 210, 45, 70));//Frame[13]
            m_rFrameWitch.Add(new Rectangle(90, 210, 45, 70));//Frame[14]
            m_rFrameWitch.Add(new Rectangle(135, 210, 45, 70));//Frame[15]

            m_ttWitchLight.m_rFrame.Add(new Rectangle(0, 0, 45, 70));
            m_ttWitchLight.m_rFrame.Add(new Rectangle(45, 0, 45, 70));
            m_ttWitchLight.m_rFrame.Add(new Rectangle(90, 0, 45, 70));
            m_ttWitchLight.m_rFrame.Add(new Rectangle(135, 0, 45, 70));
            m_ttWitchLight.m_rFrame.Add(new Rectangle(180, 0, 45, 70));
            m_ttWitchLight.m_rFrame.Add(new Rectangle(225, 0, 45, 70));
            m_ttWitchLight.m_rFrame.Add(new Rectangle(0, 70, 45, 70));
            m_ttWitchLight.m_rFrame.Add(new Rectangle(45, 70, 45, 70));
            m_ttWitchLight.m_rFrame.Add(new Rectangle(90, 70, 45, 70));
            m_ttWitchLight.m_rFrame.Add(new Rectangle(135, 70, 45, 70));
            m_ttWitchLight.m_rFrame.Add(new Rectangle(180, 70, 45, 70));
            m_ttWitchLight.m_rFrame.Add(new Rectangle(225, 70, 45, 70));

            m_rFramePet.Add(new Rectangle(0, 0, 40, 40));
            m_rFramePet.Add(new Rectangle(40, 0, 40, 40));
            m_rFramePet.Add(new Rectangle(80, 0, 40, 40));
            m_rFramePet.Add(new Rectangle(120, 0, 40, 40));

            m_rFramePet.Add(new Rectangle(0, 40, 40, 40));
            m_rFramePet.Add(new Rectangle(40, 40, 40, 40));
            m_rFramePet.Add(new Rectangle(80, 40, 40, 40));
            m_rFramePet.Add(new Rectangle(120, 40, 40, 40));

            m_rFramePet.Add(new Rectangle(160, 40, 80, 40));
            m_rFramePet.Add(new Rectangle(240, 40, 80, 40));
            m_rFramePet.Add(new Rectangle(320, 40, 80, 40));
            m_rFramePet.Add(new Rectangle(400, 40, 80, 40));

            m_rFramePet.Add(new Rectangle(160, 0, 80, 40));
            m_rFramePet.Add(new Rectangle(240, 0, 80, 40));
            m_rFramePet.Add(new Rectangle(320, 0, 80, 40));
            m_rFramePet.Add(new Rectangle(400, 0, 80, 40));

            m_ttWitchDie.m_rFrame.Add(new Rectangle(0, 0, 45, 70));
            m_ttWitchDie.m_rFrame.Add(new Rectangle(45, 0, 45, 70));
            m_ttWitchDie.m_rFrame.Add(new Rectangle(90, 0, 45, 70));
            m_ttWitchDie.m_rFrame.Add(new Rectangle(135, 0, 45, 70));
            m_ttWitchDie.m_rFrame.Add(new Rectangle(180, 0, 45, 70));
            m_ttWitchDie.m_rFrame.Add(new Rectangle(225, 0, 45, 70));
        }

        public void DiChuyen(int NewDirection, GameTime gameTime) {
            if (m_iLife > 1) {
                if (NewDirection != m_iDirection) //Người đổi hướng
                {
                    m_iDirection = NewDirection;
                    m_iIDWitch = NewDirection;
                    m_iIDPet = NewDirection;
                } else {
                    m_iCount += gameTime.ElapsedGameTime.Milliseconds;
                    if (m_iCount > 50) {
                        m_iIDWitch++;
                        m_iIDPet++;
                        if (m_iIDWitch == m_iDirection + 4) //Nếu vựt qua khung hình di chuyển quay về hình đầu
                        {
                            m_iIDWitch = m_iDirection;
                            m_iIDPet = NewDirection;
                        }
                        m_iCount = 0;
                    }
                }
                float TempSpeed = 0;
                if (m_iLife > 2)
                    TempSpeed = (float)3.5;
                else
                    TempSpeed = (float)m_dSpeed;
                switch (NewDirection) {
                    case 0:
                        m_v2Position += new Vector2(0, TempSpeed);
                        break;
                    case 4:
                        m_v2Position += new Vector2(0, -TempSpeed);
                        break;
                    case 8:
                        m_v2Position += new Vector2(TempSpeed, 0);
                        break;
                    case 12:
                        m_v2Position += new Vector2(-TempSpeed, 0);
                        break;
                    default:
                        break;
                }
            }
        }

        public void DiChuyenTaiCho(int HuongMoi, GameTime gameTime, KeyboardState keyState, int[, ,] map, int IDmap) {
            int dx, dy, dkx, dky;
            if (HuongMoi != m_iDirection) //Người đổi hướng
            {
                m_iDirection = HuongMoi;
                m_iIDWitch = HuongMoi;
                m_iIDPet = HuongMoi;
            } else {
                m_iCount += gameTime.ElapsedGameTime.Milliseconds;
                if (m_iCount > 100) {
                    m_iIDWitch++;
                    m_iIDPet++;
                    if (m_iIDWitch == m_iDirection + 4) //Nếu vựt qua khung hình di chuyển quay về hình đầu
                    {
                        m_iIDWitch = m_iDirection;
                        m_iIDPet = m_iDirection;
                    }
                    m_iCount = 0;
                }
            }
            if (keyState.IsKeyDown(Keys.Down)) {
                dx = (int)((m_v2Position.X - 12) / 40);
                dy = (int)((m_v2Position.Y) / 40);
                dkx = (int)((m_v2Position.X + 17) / 40);
                dky = (int)((m_v2Position.Y + 30) / 40);
                if ((dy < 12) && (map[IDmap, dy + 1, dx] == 0 || (map[IDmap, dy + 1, dx] > 6 && map[IDmap, dy + 1, dx] < 11))
                    && ((map[IDmap, dy, dx] == 0) || (map[IDmap, dy, dx] > 6 && map[IDmap, dy, dx] < 11))) {
                    m_v2Position += new Vector2(-2, 0);
                }
                if ((dy < 12) && (map[IDmap, dy + 1, dkx] == 0 || (map[IDmap, dy + 1, dkx] > 6 && map[IDmap, dy + 1, dkx] < 11))) {
                    m_v2Position += new Vector2(2, 0);
                }
            } else
                if (keyState.IsKeyDown(Keys.Up)) {
                    dx = (int)((m_v2Position.X - 12) / 40);
                    dy = (int)((m_v2Position.Y) / 40);
                    dkx = (int)((m_v2Position.X + 17) / 40);
                    dky = (int)((m_v2Position.Y + 30) / 40);
                    if ((dy > 0) && (map[IDmap, dy - 1, dx] == 0 || (map[IDmap, dy - 1, dx] > 6 && map[IDmap, dy - 1, dx] < 11))
                        && ((map[IDmap, dy, dx] == 0) || (map[IDmap, dy, dx] > 6 && map[IDmap, dy, dx] < 11))) {
                        m_v2Position += new Vector2(-2, 0);
                    }
                    if ((dy > 0) && (map[IDmap, dy - 1, dkx] == 0 || (map[IDmap, dy - 1, dkx] > 6 && map[IDmap, dy - 1, dkx] < 11))) {
                        m_v2Position += new Vector2(2, 0);
                    }
                } else
                    if (keyState.IsKeyDown(Keys.Right)) {
                        dx = (int)((m_v2Position.X - 10) / 40);
                        dy = (int)((m_v2Position.Y) / 40);
                        dkx = (int)((m_v2Position.X + 30) / 40);
                        dky = (int)((m_v2Position.Y + 27) / 40);
                        if ((dx <= 13) && ((map[IDmap, dy, dx + 1] == 0) || (map[IDmap, dy, dx + 1] > 6 && map[IDmap, dy, dx + 1] < 11))) {
                            m_v2Position += new Vector2(0, -2);
                        }
                        if ((dx <= 13) && (map[IDmap, dky, dx + 1] == 0 || (map[IDmap, dky, dx + 1] > 6 && map[IDmap, dky, dx + 1] < 11))
                            && (map[IDmap, dky, dx] == 0 || (map[IDmap, dky, dx] < 6 && map[IDmap, dky, dx] > 11))) {
                            m_v2Position += new Vector2(0, 2);
                        }
                    } else
                        if (keyState.IsKeyDown(Keys.Left)) {
                            dx = (int)((m_v2Position.X - 10) / 40);
                            dy = (int)((m_v2Position.Y) / 40);
                            dkx = (int)((m_v2Position.X + 30) / 40);
                            dky = (int)((m_v2Position.Y + 27) / 40);
                            if ((dx > 0) && ((map[IDmap, dy, dx - 1] == 0) || (map[IDmap, dy, dx - 1] > 6 && map[IDmap, dy, dx - 1] < 11))) {
                                m_v2Position += new Vector2(0, -2);
                            }
                            if ((dx > 0) && (map[IDmap, dky, dx - 1] == 0 || (map[IDmap, dky, dx - 1] > 6 && map[IDmap, dky, dx - 1] < 11))
                                && (map[IDmap, dky, dx] == 0 || (map[IDmap, dky, dx] < 6 && map[IDmap, dky, dx] > 11))) {
                                m_v2Position += new Vector2(0, 2);
                            }
                        }
            NotOutSideScreenWitch();
        }

        private void NotOutSideScreenWitch() {
            if (m_v2Position.X < 20 - 10)
                m_v2Position = new Vector2(20 - 10, m_v2Position.Y);
            if (m_v2Position.Y < 40 - 30)
                m_v2Position = new Vector2(m_v2Position.X, 40 - 30);
            if (m_v2Position.X > (800 - 50 - 180) + 10)
                m_v2Position = new Vector2((800 - 50 - 180) + 10, m_v2Position.Y);
            if (m_v2Position.Y > (600 - 70 - 40))
                m_v2Position = new Vector2(m_v2Position.X, (600 - 70 - 40));
        }

        protected KeyboardState CheckKeyboard(GameTime gameTime, KeyboardState keyboardState, Vector2 newPosition, int[, ,] map, int IDmap) {
            int m_iy, m_ix;
            m_iy = ((int)newPosition.X - 20) / 40;
            m_ix = ((int)newPosition.Y - 40) / 40;
            if ((keyboardState.IsKeyDown(Keys.Z)) && (m_ksPreKeyState.IsKeyUp(Keys.Z)) && (Game1.g_iEnergy > 0)) {
                if (map[IDmap, m_ix, m_iy] == 0) {
                    Game1.g_iEnergy--;
                    map[IDmap, m_ix, m_iy] = 2;
                    return keyboardState;
                }
            }
            if ((keyboardState.IsKeyDown(Keys.X)) && (m_ksPreKeyState.IsKeyUp(Keys.X)) && (Game1.g_iEnergy > 0)) {
                if (map[IDmap, m_ix, m_iy] == 0) {
                    Game1.g_iEnergy--;
                    map[IDmap, m_ix, m_iy] = 3;
                    return keyboardState;
                }
            }
            if ((keyboardState.IsKeyDown(Keys.C)) && (m_ksPreKeyState.IsKeyUp(Keys.C)) && (Game1.g_iEnergy > 0)) {
                if (map[IDmap, m_ix, m_iy] == 0) {
                    Game1.g_iEnergy--;
                    map[IDmap, m_ix, m_iy] = 4;
                    return keyboardState;
                }
            }
            if ((keyboardState.IsKeyDown(Keys.V)) && (m_ksPreKeyState.IsKeyUp(Keys.V)) && (Game1.g_iEnergy > 0)) {
                if (map[IDmap, m_ix, m_iy] == 0) {
                    Game1.g_iEnergy--;
                    map[IDmap, m_ix, m_iy] = 5;
                    return keyboardState;
                }
            }
            return keyboardState;
        }

        public void Update(GameTime gameTime, KeyboardState keyState, int[, ,] map, int ID, ManagerEnemies managerEnemies) {
            if (Game1.g_bPause == false) {
                int dx = (int)((m_v2Position.X - 10) / 40);
                int dy = (int)((m_v2Position.Y) / 40);
                int dkx = (int)((m_v2Position.X + 17) / 40);
                int dky = (int)((m_v2Position.Y + 27) / 40);
                #region RNext
                int[] RNext = new int[4];
                if (dy == 12)
                    RNext[0] = 1;
                else
                    RNext[0] = map[ID, dy + 1, dx]; // 0

                if (dy == 0)
                    RNext[1] = 1;
                else
                    RNext[1] = map[ID, dy - 1, dx]; // 4

                if (dx == 14)
                    RNext[2] = 1;
                else
                    RNext[2] = map[ID, dy, dx + 1]; // 8

                if (dx == 0)
                    RNext[3] = 1;
                else
                    RNext[3] = map[ID, dy, dx - 1]; // 12
                #endregion

                if (keyState.IsKeyDown(Keys.Down)) {
                    dx = (int)((m_v2Position.X - 10) / 40);
                    dy = (int)((m_v2Position.Y) / 40);
                    dkx = (int)((m_v2Position.X + 17) / 40);
                    dky = (int)((m_v2Position.Y + 30) / 40);
                    #region Dichuyen
                    #region RNext
                    if (dy == 12)
                        RNext[0] = 1;
                    else
                        RNext[0] = map[ID, dy + 1, dx]; // 0
                    #endregion

                    if ((RNext[0] == 0 || (RNext[0] > 6 && RNext[0] < 11)) && dkx == dx)
                        DiChuyen(0, gameTime);
                    else
                        if ((40 * (dy - 1) + 40) - (m_v2Position.Y - 10) > 0)
                            DiChuyen(0, gameTime);
                        else
                            if ((dkx >= 1 && dky <= 12) && (map[ID, dky, dkx] == 0 || (map[ID, dky, dkx] > 6 && map[ID, dky, dkx] < 11)) &&
                                (map[ID, dky, dkx - 1] == 0 || (map[ID, dky, dkx - 1] > 6 && map[ID, dky, dkx - 1] < 11)) &&
                                (map[ID, dy + 1, dx + 1] == 0 || (map[ID, dy + 1, dx + 1] > 6 && map[ID, dy + 1, dx + 1] < 11)))
                                DiChuyen(0, gameTime);
                            else
                                DiChuyenTaiCho(0, gameTime, keyState, map, ID);
                } else if (keyState.IsKeyDown(Keys.Up)) {
                    if (((RNext[1] > 6 && RNext[1] < 11) || RNext[1] == 0) && dkx == dx)
                        DiChuyen(4, gameTime);
                    else
                        if ((m_v2Position.Y - 10) - (40 * (dy - 1) + 40) > 0)
                            DiChuyen(4, gameTime);
                        else
                            if ((dkx >= 1 && dky >= 1) && (map[ID, dky - 1, dkx] == 0 || (map[ID, dky - 1, dkx] > 6 && map[ID, dky - 1, dkx] < 11)) &&
                                ((map[ID, dky - 1, dkx - 1] == 0) || (map[ID, dky - 1, dkx - 1] > 6 && map[ID, dky - 1, dkx - 1] < 11)))
                                DiChuyen(4, gameTime);
                            else
                                DiChuyenTaiCho(4, gameTime, keyState, map, ID);
                } else if (keyState.IsKeyDown(Keys.Right)) {
                    dx = (int)((m_v2Position.X - 10) / 40);
                    dy = (int)((m_v2Position.Y) / 40);
                    dkx = (int)((m_v2Position.X + 20) / 40);
                    dky = (int)((m_v2Position.Y + 27) / 40);

                    #region RNext
                    if (dx == 14)
                        RNext[2] = 1;
                    else
                        RNext[2] = map[ID, dy, dx + 1]; // 8
                    #endregion
                    if (((RNext[2] > 6 && RNext[2] < 11) || RNext[2] == 0) && dky == dy)
                        DiChuyen(8, gameTime);
                    else
                        if ((40 * dx + 20) - m_v2Position.X > 0)
                            DiChuyen(8, gameTime);
                        else
                            if ((dkx <= 14 && dky >= 1 && dky < 13) && (map[ID, dky, dkx] == 0 || (map[ID, dky, dkx] > 6 && map[ID, dky, dkx] < 11)) &&
                                (map[ID, dky - 1, dkx] == 0 || (map[ID, dky - 1, dkx] > 6 && map[ID, dky - 1, dkx] < 11)))
                                DiChuyen(8, gameTime);
                            else
                                DiChuyenTaiCho(8, gameTime, keyState, map, ID);
                } else if (keyState.IsKeyDown(Keys.Left)) {
                    if (((RNext[3] > 6 && RNext[3] < 11) || RNext[3] == 0) && dky == dy)
                        DiChuyen(12, gameTime);
                    else
                        if (m_v2Position.X - (40 * dx + 20) > 0)
                            DiChuyen(12, gameTime);
                        else
                            if ((dkx >= 1 && dky >= 1 && dky < 13) && (map[ID, dky, dkx - 1] == 0 || (map[ID, dky, dkx - 1] > 6 && map[ID, dky, dkx - 1] < 11)) &&
                                ((map[ID, dky - 1, dkx - 1] == 0) || (map[ID, dky - 1, dkx - 1] > 6 && map[ID, dky - 1, dkx - 1] < 11)))
                                DiChuyen(12, gameTime);
                            else
                                DiChuyenTaiCho(12, gameTime, keyState, map, ID);
                }
                    #endregion
                Rectangle m_rWitchRect = new Rectangle(
                                    (int)m_v2Position.X + 13,
                                    (int)m_v2Position.Y + 35,
                                    20,
                                    20);
                foreach (cEnemies enemie in managerEnemies.l_Enemies)
                    if (enemie.m_bAlive && (enemie.m_iIDlife == 2 || enemie.m_iIDlife == 3)) {
                        Rectangle m_rBudRect = new Rectangle(
                                    (int)enemie.m_v2Position.X + 10,
                                    (int)enemie.m_v2Position.Y + 10,
                                    20,
                                    20);
                        if (m_rWitchRect.Intersects(m_rBudRect) && m_ttWitchLight.m_iIDFrame > 11) {
                            m_iLife = 1;
                        }
                    }
                m_ksPreKeyState = CheckKeyboard(gameTime, keyState, Game1.Chuan(m_v2Position + new Vector2(5, 30)), map, ID);
            }
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime, ref bool pause, ManagerMap map) {
            float m_fBGLayerDepth = m_v2Position.Y / 600.0f;
            switch (m_iLife) {
                case 1:
                    m_ttWitchDie.m_iTimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                    if (m_ttWitchDie.m_iTimeSinceLastFrame > m_ttWitchDie.m_iMilliSecondsPerFrame) {
                        m_ttWitchDie.m_iIDFrame++;
                        m_ttWitchDie.m_iTimeSinceLastFrame -= m_ttWitchDie.m_iMilliSecondsPerFrame;
                    }
                    if (m_ttWitchDie.m_iIDFrame < 6)
                        spriteBatch.Draw(m_ttWitchDie.m_tWitch, m_v2Position,
                                    m_ttWitchDie.m_rFrame[m_ttWitchDie.m_iIDFrame],
                                    Color.White, 0, Vector2.Zero,
                                    1, SpriteEffects.None, m_fBGLayerDepth);
                    else {
                        Game1.g_iLife--;
                        if (Game1.g_iLife > 0) {
                            m_ttWitchDie.m_iIDFrame = 0;
                            m_iLife = 2;
                            m_ttWitchLight.m_iIDFrame = 0;
                        } else {
                            map.m_iTimeStatus = 0;
                            map.m_iIDStatus = 3;
                            pause = true;
                            m_iLife = 0;
                        }
                    }
                    break;
                case 2:
                    if (pause == false)
                        m_ttWitchLight.m_iTimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                    if (m_ttWitchLight.m_iTimeSinceLastFrame > m_ttWitchLight.m_iMilliSecondsPerFrame) {
                        m_ttWitchLight.m_iIDFrame++;
                        m_ttWitchLight.m_iTimeSinceLastFrame -= m_ttWitchLight.m_iMilliSecondsPerFrame;
                    }
                    spriteBatch.Draw(m_tTexture, m_v2Position, m_rFrameWitch[m_iIDWitch], Color.White, 0, Vector2.Zero,
                                        1, SpriteEffects.None, m_fBGLayerDepth); //Bạn nhớ ở đây cho Frame[ID] vào nha
                    if (m_ttWitchLight.m_iIDFrame < 12)
                        spriteBatch.Draw(m_ttWitchLight.m_tWitch, m_v2Position,
                                        m_ttWitchLight.m_rFrame[m_ttWitchLight.m_iIDFrame],
                                        Color.White, 0, Vector2.Zero,
                                        1, SpriteEffects.None, 1);
                    break;
                case 3:
                    m_ttWitchLight.m_iTimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                    if (m_ttWitchLight.m_iTimeSinceLastFrame > m_ttWitchLight.m_iMilliSecondsPerFrame) {
                        m_ttWitchLight.m_iIDFrame++;
                        m_ttWitchLight.m_iTimeSinceLastFrame -= m_ttWitchLight.m_iMilliSecondsPerFrame;
                    }
                    spriteBatch.Draw(m_tTexture, m_v2Position, m_rFrameWitch[m_iIDWitch], Color.White, 0, Vector2.Zero,
                                        1, SpriteEffects.None, m_fBGLayerDepth); //Bạn nhớ ở đây cho Frame[ID] vào nha
                    if (m_iIDPet < 8) {
                        m_fBGLayerDepth = (m_v2Position.Y + 4) / 600.0f;
                        spriteBatch.Draw(m_tPet, m_v2Position + new Vector2(4, 26), m_rFramePet[m_iIDPet], Color.White, 0, Vector2.Zero,
                                        1, SpriteEffects.None, m_fBGLayerDepth);
                    } else {
                        m_fBGLayerDepth = (m_v2Position.Y + 28) / 600.0f;
                        spriteBatch.Draw(m_tPet, m_v2Position - new Vector2(15, -28), m_rFramePet[m_iIDPet], Color.White, 0, Vector2.Zero,
                                        1, SpriteEffects.None, m_fBGLayerDepth);
                    }
                    if (m_ttWitchLight.m_iIDFrame < 12)
                        spriteBatch.Draw(m_ttWitchLight.m_tWitch, m_v2Position,
                                        m_ttWitchLight.m_rFrame[m_ttWitchLight.m_iIDFrame],
                                        Color.White, 0, Vector2.Zero,
                                        1, SpriteEffects.None, m_fBGLayerDepth);
                    break;
                default:
                    break;
            }
        }
    }
}